/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package facade;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import local.AttemptFacadeLocal;
import model.Attempt;
import model.PublicUser;
import model.Scoring;
import model.ThemeScore;

/**
 *
 * @author Ken
 */
@Stateless
public class AttemptFacade implements AttemptFacadeLocal {

    @PersistenceContext(unitName = "IEMPPU")
    private EntityManager em;

    @Override
    public void create(Attempt a) {
        em.persist(a);
    }

    @Override
    public void edit(Attempt a) {
        em.merge(a);
    }

    @Override
    public void refresh(Attempt a) {
        em.refresh(a);
    }

    @Override
    public void updateAttempt(Long attempt_id, String status) {
        Attempt a = em.find(Attempt.class, attempt_id);

        a.setStatus(Attempt.SurveyStatus.valueOf(status));
        em.merge(a);
    }

    @Override
    public ArrayList<Attempt> getAttempts(Long industry, String status) {
        Query query = em.createQuery("SELECT a FROM Attempt a WHERE a.industry.ind_id=?1 AND a.status=?2");
        query.setParameter(1, industry);
        query.setParameter(2, Attempt.SurveyStatus.valueOf(status));

        ArrayList<Attempt> aList = new ArrayList<Attempt>();

        for (Object obj : query.getResultList()) {
            Attempt a = (Attempt) obj;
            aList.add(a);
        }

        return aList;
    }

     @Override
    public int getTotalResponse(Date exp_date_1, Date exp_date_2, String industryID) {
        Long indID = Long.parseLong(industryID);
        String queryStr = "";
        Query query;
        queryStr = "SELECT a FROM Attempt a WHERE a.industry.ind_id=?1 "
                + "AND a.timestamps BETWEEN ?2 AND ?3 "
                + "ORDER BY a.timestamps";

        query = em.createQuery(queryStr);
        query.setParameter(1, indID);
        query.setParameter(2, exp_date_1);
        query.setParameter(3, exp_date_2);

       
        return query.getResultList().size();
    }

    @Override
    public int getTotalCompletedResponse(Date exp_date_1, Date exp_date_2, String industryID) {
        Long indID = Long.parseLong(industryID);
        String queryStr = "";
        Query query;
        queryStr = "SELECT a FROM Attempt a WHERE a.industry.ind_id=?1 "
                + "AND a.status=?2 "
                + "AND a.timestamps BETWEEN ?3 AND ?4 "
                + "ORDER BY a.timestamps";

        query = em.createQuery(queryStr);
        query.setParameter(1, indID);
        query.setParameter(2, Attempt.SurveyStatus.Completed);
        query.setParameter(3, exp_date_1);
        query.setParameter(4, exp_date_2);

       
        return query.getResultList().size();
    }

    @Override
    public int getTotalAcceptedResponse(Date exp_date_1, Date exp_date_2, String industryID) {
        

        Long indID = Long.parseLong(industryID);
        String queryStr = "";
        Query query;
        queryStr = "SELECT a FROM Attempt a WHERE a.industry.ind_id=?1 "
                + "AND a.status=?2 "
                + "AND a.timestamps BETWEEN ?3 AND ?4 "
                + "ORDER BY a.timestamps";

        query = em.createQuery(queryStr);
        query.setParameter(1, indID);
        query.setParameter(2, Attempt.SurveyStatus.Acceptable);
        query.setParameter(3, exp_date_1);
        query.setParameter(4, exp_date_2);

       
        return query.getResultList().size();
    }
    
    @Override
    public List<Attempt> getTotalAcceptedResponseList(Date exp_date_1, Date exp_date_2, String industryID) {
        

        Long indID = Long.parseLong(industryID);
        String queryStr = "";
        Query query;
        queryStr = "SELECT a FROM Attempt a WHERE a.industry.ind_id=?1 "
                + "AND a.status=?2 "
                + "AND a.timestamps BETWEEN ?3 AND ?4 "
                + "ORDER BY a.timestamps";

        query = em.createQuery(queryStr);
        query.setParameter(1, indID);
        query.setParameter(2, Attempt.SurveyStatus.Acceptable);
        query.setParameter(3, exp_date_1);
        query.setParameter(4, exp_date_2);

       
        return query.getResultList();
    }
    
    @Override
    public List<Attempt> getTotalResponseList(Date exp_date_1, Date exp_date_2, String industryID) {
        

        Long indID = Long.parseLong(industryID);
        String queryStr = "";
        Query query;
        queryStr = "SELECT a FROM Attempt a WHERE a.industry.ind_id=?1 "
                + "AND a.timestamps BETWEEN ?2 AND ?3 "
                + "ORDER BY a.timestamps";

        query = em.createQuery(queryStr);
        query.setParameter(1, indID);
        query.setParameter(2, exp_date_1);
        query.setParameter(3, exp_date_2);

       
        return query.getResultList();
    }
    
    @Override
    public List<Attempt> getTotalCompletedResponseList(Date exp_date_1, Date exp_date_2, String industryID) {
        

        Long indID = Long.parseLong(industryID);
        String queryStr = "";
        Query query;
        queryStr = "SELECT a FROM Attempt a WHERE a.industry.ind_id=?1 "
                + "AND a.status=?2 "
                + "AND a.timestamps BETWEEN ?3 AND ?4 "
                + "ORDER BY a.timestamps";

        query = em.createQuery(queryStr);
        query.setParameter(1, indID);
        query.setParameter(2, Attempt.SurveyStatus.Completed);
        query.setParameter(3, exp_date_1);
        query.setParameter(4, exp_date_2);

       
        return query.getResultList();
    }


    @Override
    public void deleteAttempt(Long attempt_id) {
        Attempt a = em.find(Attempt.class, attempt_id);
        
        Collection<Scoring> sList = a.getScoring();
        Collection<ThemeScore> tsList = a.getThemeScore();
        
        a.setScoring(null);
        a.setThemeScore(null);
        
        for (Scoring s : sList) {
            em.remove(s);
        }
        
        for (ThemeScore ts : tsList) {
            em.remove(ts);
        }
        
        PublicUser pu = em.find(PublicUser.class, a.getPu_id());
        pu.getAttempt().remove(a);
        
        if (pu.getAttempt().isEmpty()) {
            em.remove(pu);
        } else {
            em.merge(pu);
        }
        
        em.remove(a);
    }

    @Override
    public Attempt getAttempt(Long attempt_id) {
        Attempt a = em.find(Attempt.class, attempt_id);
        
        return a;
    }
    
    
    @Override
    public Attempt getAttemptByLink(String link) {
        Query query = em.createQuery("SELECT a FROM Attempt a WHERE a.link=?1");
        query.setParameter(1, link);
        
        ArrayList<Attempt> aList = new ArrayList<Attempt>(query.getResultList());

        if(!aList.isEmpty())
            return aList.get(0);
        else
            return null;
        
    }
    
}
